package com.kaluli.system.utils;

import java.util.List;


/**
 * 分页组件的实现
 * @author Louis Huang
 * @since 1.0
 * @param <T>
 */
public class DefaultPage<T> implements Pageable<T> {

	private List<T> content;
	private Long total; 		// 总页数
	private Long number; 		// 当前页数
	private Long numberPerPage; // 每页多少条
	
	public DefaultPage(List<T> content, long total) {
		this( content , total , 0 , 10);
	}
	
	public DefaultPage(List<T> content, long total , long numberPerpage) {
		this( content , total , 0 , numberPerpage);
	}
	
	public DefaultPage(List<T> content, long total , long number, long numberPerpage) {
		this.content = content;
		this.total = total;
		this.number = number;
		this.numberPerPage = numberPerpage;
	}

	@Override
	public int getNumber() {
		return this.number.intValue();
	}

	@Override
	public int getTotal() {
		Long size = this.total / this.numberPerPage;
		if (this.total % this.numberPerPage != 0) {
			size += 1;
		}
		return size.intValue();
	}

	@Override
	public int getNumberPerPage() {
		return this.numberPerPage.intValue();
	}

	@Override
	public int getTotalOfElements() {
		return this.total.intValue();
	}

	@Override
	public int getNumberOfElements() {
		return  this.getNumber() * this.getNumberPerPage();
	}

	@Override
	public int getNumberEndOfElements() {
		return  getNumberOfElements() + this.getContent().size();
	}

	@Override
	public List<T> getContent() {
		return this.content;
	}

	@Override
	public boolean hasContent() {
		return this.content != null && this.content.size() > 0;
	}

	@Override
	public boolean isFirst() {
		return this.number == 0;
	}

	@Override
	public boolean isLast() {
		return this.number ==  this.getTotal();
	}

	@Override
	public boolean hasNext() {
		return this.number + 1 < this.getTotal();
	}

	@Override
	public boolean hasPrevious() {
		return this.number > 0;
	}

}
